<?php
include '../JHeader.php';
require_once('../Database/Database.php');

if(isset($_GET['id']))
{
    $id = $_GET['id'];

    $db = new Database();
    $db->Connect();
    $shortcut = $db->SelectColumns('Series', 'Shortcut', 'ID_Series = ' . $id);
    if($shortcut)
    {
        $shortcut = $shortcut[0]->Shortcut;
        /**
        * Überprüft, ob der Fahrer bereits angemeldet ist.
        *
        * @author Timo
        */
        function CheckRegistration($db_drivers, $joomla_id)
        {
            $db = new Database();
            $db->Connect();

            if($db->Select($db_drivers, 'JID = ' . $joomla_id))
            {
                return true;
            }
            else
            {
                return false;
            }
            $db->Disconnect();
        }

        /**
        * Gibt eine Liste mit allen freien Fahrernummern zurück.
        *
        * @author Timo
        */
        function GetUsedNumbers($db_table)
        {
            $db  = new Database();
            $db->Connect();

            $used_numbers = array();
            $data         = $db->SelectColumns($db_table, 'Number');
            $db->Disconnect();

            if($data)
            {
                foreach($data as $d)
                {
                    $used_numbers[] = $d->Number;
                }
            }
            else
            {
                $data = 0;
            }
            return $used_numbers;
        }

        /**
        * Gibt eine Liste mit Teams zurück, in denen der Fahrer sich befindet..
        *
        * @author Timo
        */
        function GetTeams($db_teams)
        {
            $db  = new Database();
            $db->Connect();

            $teams = $db->SelectColumns($db_teams, "ID_Team, Name");

            $db->Disconnect();

            if(!$teams)
            {
                $teams = 0;
            }
            return $teams;
        }

        /**
        * Gibt eine Liste mit allen Fahrzeugen in der Serie zurück..
        *
        * @author Timo
        */
        function GetAllVehicles($db_vehicles)
        {
            $db  = new Database();
            $db->Connect();

            $vehicles = $db->Select($db_vehicles);

            $db->Disconnect();

            return $vehicles;
        }

        function GetSeriesName($series_id)
        {
            $db  = new Database();
            $db->Connect();
            $series = $db->SelectColumns('Series', 'Name', 'ID_Series = ' . $series_id);
            $db->Disconnect();

            if($series)
            {
                return $series[0]->Name;
            }
            else
            {
                return 'N/A';
            }
        }

        function SteamNeeded($series_id)
        {
            $db  = new Database();
            $db->Connect();
            $series = $db->SelectColumns('Series', 'Steam', 'ID_Series = ' . $series_id);
            $db->Disconnect();

            if($series)
            {
                if($series[0]->Steam == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return -1;
            }
        }

        $series_name = GetSeriesName($id);
        if(CheckRegistration($shortcut . '_drivers', $jid))
        {
            echo "Du bist bereits angemeldet!";
        }
        else // SIGN-UP
        {
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <script type="text/javascript" language="JavaScript" src="js/check.js"></script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Anmeldung - "<?php echo $series_name?>"</title>
    </head>
    <body onLoad="GetInfo(<?php echo '\'' . $shortcut . '\'' ?>);">
        <div id="header"><h1>Anmeldung - "<?php echo $series_name?>"</h1></div>
        <form name="signup" action="process_sign.php" method="post">
            <div class="constant_values">
                <table>
                    <tr>
                        <td>Fahrername</td>
                        <td>
                            <input type="text" name="driver_name" size="25" maxlength="50" value="<?php echo $jname ?>" readonly />
                            <input type="hidden" name="joomla_id" size="3" maxlength="5" value="<?php echo $jid ?>" />
                        </td>
                        <td id="check_name"></td>
                    </tr>
                </table>
            </div>
            <div class="changeable_values">
                <table>
                    <?php if(SteamNeeded($id))
                    { ?>
                        <tr>
                            <td>Steamname</td>
                            <td><input type="text" name="steam_name" size="25" maxlength="50" onkeyup="CheckSteamName(<?php echo '\'' . $shortcut . '\'' ?>);" /></td>
                            <td id="check_steamname"></td>
                        </tr>
              <?php } ?>
                    <tr>
                        <td>Fahrernummer</td>
                        <td>
                            <select name="select_number" size="1">
                                    <?php
                                    $used = GetUsedNumbers($shortcut . '_drivers');
                                    for($i = 1; $i < 100; $i++)
                                    {
                                        if(!in_array($i, $used))
                                        {
                                            echo '<option value="' . $i . '">' . $i . '</option>"';
                                        }
                                    }
                                    ?>
                            </select>
                        </td>
                        <td id="check_number"></td>
                        </tr>
                        <tr>
                            <td>Team</td>
                            <td>
                                <select name="select_team" size="1" onchange="GetTeamInfo(<?php echo '\'' . $shortcut . '\'' ?>);">
                                    <?php
                                    $teams = GetTeams($shortcut . '_teams');
                                    if($teams)
                                    {
                                        foreach($teams as $team)
                                        {
                                            echo '<option value="' . $team->ID_Team . '">' . $team->Name . '</option>';
                                        }
                                    }
                                    else
                                    {
                                        echo '<option value="None" selected>Keine Teams</option>';
                                    }
                                    ?>
                                </select>
                            </td>
                            <td></td>
                        </tr>
                        <tr>
                            <td>Fahrzeug</td>
                            <td>
                                <?php
                                $vehicles = GetAllVehicles($shortcut . '_vehicles');
                                // TODO: Wenn nur ein Fahrzeug pro Team erlaubt ist..
                                //  .. wird zwangsweise das bereits vom Team gewählte Fahrzeug selektiert.
                                ?>
                                <select name="select_vehicle" size="1" onchange="GetVehicleInfo(<?php echo '\'' . $shortcut . '\'' ?>);">
                                    <?php
                                    if($vehicles)
                                    {
                                        foreach($vehicles as $vehicle)
                                        {
                                            echo '<option value="' . $vehicle->ID_Vehicle . '">' . $vehicle->Name . '</option>';
                                        }
                                    }
                                    else
                                    {
                                        echo '<option value="None">N/A</option>';
                                    }
                                    ?>
                                </select>
                            </td>
                            <td></td>
                        </tr>
                </table>
            </div>
            <div class="extra">
                <div><a href="new_team.php?id=<?php echo $id ?>">Neues Team erstellen..</a></div>
                <div id="teaminfo"></div>
                <div id="vehicleinfo"></div>
            </div>
            <div>
                <input type="hidden" name="series_id" value="<?php echo $id ?>" />
                <input type="submit" name="sign" value="Anmelden" />
            </div>
        </form>
    </body>
</html>
<?php
        }
    }
    else
    {
        echo 'Ung&uuml;ltige Serien ID.';
    }
}
else
{
    echo 'ERROR';
}
?>